package com.wanmao.test.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wanmao.test.R;
import com.wanmao.test.model.DefectModel;

import java.util.List;


public class DefectDetailAdapter extends BaseAdapter {
    Context context;
    List<DefectModel> list;
    LayoutInflater layoutInflater;

    public DefectDetailAdapter(Context context, List<DefectModel> list) {
        this.context = context;
        this.list = list;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return null == list ? 0 : list.size();
    }

    @Override
    public DefectModel getItem(int position) {
        return null == list ? null : list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (null == convertView) {
            convertView = layoutInflater.inflate(R.layout.item_defect_list, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.init(getItem(position));
        return convertView;
    }

    class ViewHolder {
        TextView title, content;

        public ViewHolder(View convertView) {
            title = (TextView) convertView.findViewById(R.id.tv_title);
            content = (TextView) convertView.findViewById(R.id.tv_content);
        }

        public void init(DefectModel item) {
            title.setText(item.getTitle());
            content.setText(item.getContent());
        }
    }
}
